<template>
  <el-dialog
    title="提示"
    :model-value="modelValue"
    @closed="closed"
    width="22%"
  >
    <div class="center">
      <p class="title">{{ $t('msg.theme.themeColorChange') }}</p>
      <el-color-picker
        v-model="mColor"
        :predefine="predefineColors"
      ></el-color-picker>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closed">{{ $t('msg.universal.cancel') }}</el-button>
        <el-button type="primary" @click="confirm">{{
          $t('msg.universal.confirm')
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineProps, defineEmits, ref } from 'vue'
import { generateNewStyle, writeNewStyle } from '@/utils/theme'
import { useStore } from 'vuex'
const store = useStore()
defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})

const emits = defineEmits(['update:modelValue'])
const mColor = ref(store.getters.mainColor)
const predefineColors = ref([
  '#409eff',
  '#ff4949',
  '#ffba00',
  '#00d294',
  '#00a2ae',
  '#a0a0a0',
  '#e6e6e6',
  '#fff'
])

const closed = () => {
  emits('update:modelValue', false)
}
const confirm = async () => {
  // 获取主题色
  const newStyleText = await generateNewStyle(mColor.value)
  // 写入最新主题色
  writeNewStyle(newStyleText)
  store.commit('theme/setMainColor', mColor.value)
  closed()
}
</script>

<style lang="scss" scoped>
.center {
  text-align: center;
  .title {
    margin-bottom: 12px;
  }
}
</style>
